package demoMod.invincibleOne.neow;

import com.megacrit.cardcrawl.random.Random;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class InvincibleReward {
    private final List<InvincibleRewardItem> items = new ArrayList<>();

    public void addItem(InvincibleRewardItem item) {
        items.add(item);
    }

    public String getDescription() {
        StringBuilder sb = new StringBuilder();
        for (InvincibleRewardItem item : items) {
            if (sb.length() > 0) {
                sb.append(" ");
            }
            if (item.isNegative()) {
                sb.append("#r");
                sb.append(item.getDescription().replace(" ", " #r"));
            } else {
                sb.append("#g");
                sb.append(item.getDescription().replace(" ", " #g"));
            }
        }
        return sb.toString();
    }

    public void activate() {
        for (InvincibleRewardItem item : items) {
            item.activate();
        }
    }

    public void update() {
        for (InvincibleRewardItem item : items) {
            item.update();
        }
    }

    public static List<InvincibleReward> getRandomReward(Random rng, int size) {
        List<InvincibleReward> ret = new ArrayList<>();
        List<InvincibleRewardItem> weakPositivePool = Arrays.stream(InvincibleRewardItem.values()).filter(item -> !item.isNegative() && !item.isStrongPositive()).collect(Collectors.toList());
        List<InvincibleRewardItem> strongPositivePool = Arrays.stream(InvincibleRewardItem.values()).filter(InvincibleRewardItem::isStrongPositive).collect(Collectors.toList());
        List<InvincibleRewardItem> negativePool = Arrays.stream(InvincibleRewardItem.values()).filter(InvincibleRewardItem::isNegative).collect(Collectors.toList());
        Collections.shuffle(weakPositivePool, new java.util.Random(rng.randomLong()));
        Collections.shuffle(negativePool, new java.util.Random(rng.randomLong()));
        for (int i=0;i<size;i++) {
            InvincibleReward reward = new InvincibleReward();
            if (i <= 1) {
                reward.addItem(weakPositivePool.remove(0));
            } else {
                reward.addItem(negativePool.remove(0));
                if (reward.getItems().get(0) == InvincibleRewardItem.NO_GOLD) {
                    strongPositivePool.remove(InvincibleRewardItem.GAIN_250_GOLD);
                }
                reward.addItem(strongPositivePool.remove(0));
            }
            ret.add(reward);
        }
        return ret;
    }

    public static List<InvincibleReward> getRandomMiniReward(Random rng, int size) {
        List<InvincibleReward> ret = new ArrayList<>();
        List<InvincibleRewardItem> weakPositivePool = Arrays.stream(InvincibleRewardItem.values()).filter(item -> !item.isNegative() && !item.isStrongPositive()).collect(Collectors.toList());
        Collections.shuffle(weakPositivePool, new java.util.Random(rng.randomLong()));
        for (int i=0;i<size;i++) {
            InvincibleReward reward = new InvincibleReward();
            reward.addItem(weakPositivePool.remove(0));
            ret.add(reward);
        }
        return ret;
    }

    public List<InvincibleRewardItem> getItems() {
        return items;
    }
}
